<template>
  <div>
    <!-- 使用isAdd 展示添加还是编辑 -->
    <el-dialog :title="info.isAdd?'活动添加':'活动编辑'" :visible.sync="info.isshow" @closed="closed" @opened="opened">
      {{form}}
      <el-form :model="form">
        <!-- 2.绑定数据 -->
        <el-form-item label="活动名称" label-width="120px">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动期限" label-width="120px">
          
            <div class="block">
              <el-date-picker v-model="form.begintime" type="date" placeholder="开始日期" value-format="timestamp">></el-date-picker>
            </div>
            <div class="block">
              <el-date-picker v-model="form.endtime" type="date" placeholder="结束日期" value-format="timestamp">></el-date-picker>
            </div>
         
        </el-form-item>
        <el-form-item label="一级分类" label-width="120px">
          <el-select placeholder="请选择" v-model="form.first_cateid" @change="changeFirstCateId">
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" label-width="120px">
          <el-select placeholder="请选择" v-model="form.second_cateid" @change="changeSecondCateId">
            <el-option
              v-for="item in secondCateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品" label-width="120px">
          <el-select placeholder="请选择" v-model="form.goodsid">
            <el-option
              v-for="item in goodsList"
              :key="item.id"
              :label="item.goodsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  reqcatelist,
  reqgoodslist,
  reqseckadd,
  reqseckinfo,
  reqseckedit,
} from "../../../request/api";
import { errorAlert, successAlert } from "../../../utils/alert";
import path from "path";
import { mapGetters, mapActions } from "vuex";

export default {
  components: {},
  props: ["info"],
  data() {
    return {
      // 1.初始化数据
      form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 2,
      },

      //二级分类的列表
      secondCateList: [],
      //三级商品的列表
      goodsList: [],
    };
  },
  computed: {
    ...mapGetters({
      cateList: "cate/list", //分类列表
    }),
  },
  mounted() {
    //3.请分类列表
    if (this.cateList.length == 0) {
      this.reqcateList();
    }
  },
  methods: {
    ...mapActions({
      reqcateList: "cate/reqList", //请求分类列表

      reqList: "seckill/reqList",
    }),

    // 5.修改了1级分类id
    changeFirstCateId() {
      //清空二级的数据
      this.form.second_cateid = "";

      //获取二级列表
      this.getSecondCateList();
    },
    getSecondCateList() {
      //根据1级 的id取二级分类的列表
      reqcatelist({
        pid: this.form.first_cateid,
      }).then((res) => {
        if (res.data.code == 200) {
          this.secondCateList = res.data.list ? res.data.list : [];
        }
      });
    },
    //6修改了2级分类id
    changeSecondCateId() {
      //清空二级的数据
      this.form.goodsid = "";

      //获取san级列表
      this.getGoodsList();
      console.log(this.goodsList);
    },
    getGoodsList() {
      //根据2级 的id取三级商品的列表
      reqgoodslist({
        fid: this.form.first_cateid,
        sid: this.form.second_cateid,
      }).then((res) => {
        if (res.data.code == 200) {
          this.goodsList = res.data.list ? res.data.list : [];
        }
      });
    },
    //一、创建编辑器
    opened() {
   
    },

    //取消
    cancel() {
      this.info.isshow = false;
    },
    //弹框关闭
    closed() {
      if (!this.info.isAdd) {
        this.empty();
      }
    },
    //重置form
    empty() {
      this.form = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 2,
      };

      //二级分类的列表
      this.secondCateList = [];
    },
    //验空的函数
    checkEmpty() {
      // return new Promise((resolve, reject) => {
      //   if (this.form.first_cateid == "") {
      //     errorAlert("一级分类不能为空");
      //     return;
      //   }
      //   if (this.form.second_cateid == "") {
      //     errorAlert("二级分类不能为空");
      //     return;
      //   }
      //   if (this.form.goodsname == "") {
      //     errorAlert("商品名称不能为空");
      //     return;
      //   }
      //   if (this.form.price == "") {
      //     errorAlert("价格不能为空");
      //     return;
      //   }
      //   // if (this.form.market_price == "") {
      //   //   errorAlert("市场价格不能为空");
      //   //   return;
      //   // }
      //   if (!this.form.img) {
      //     errorAlert("图片不能为空");
      //     return;
      //   }
      //   if (this.form.specsid == "") {
      //     errorAlert("规格不能为空");
      //     return;
      //   }
      //   if (this.form.specsattr.length == 0) {
      //     errorAlert("规格属性不能为空");
      //     return;
      //   }
      //   if (this.editor.txt.html() === "") {
      //     errorAlert("描述不能空");
      //     return;
      //   }
      //   resolve();
      // });
    },
    //添加
    add() {
      //验空
      // this.checkEmpty().then(() => {
      // });
      // 数据交互
      reqseckadd({ ...this.form }).then((res) => {
        if (res.data.code == 200) {
          //成功了
          // （1）弹成功
          successAlert(res.data.msg);
          // (2)弹框消失
          this.cancel();
          // (3)清空数据
          this.empty();
          // (4)刷新列表
          this.reqList();
        }
      });
    },

    //获取一条数据
    getOne(id) {
      reqseckinfo({ id: id }).then((res) => {

        console.log(res);
        if (res.data.code == 200) {
          this.form = res.data.list;
          //（1）补id
          this.form.id = id;
          // (2)获取二级列表
          this.getSecondCateList();
          // (3)获取3级列表
          this.getGoodsList();
          
        }
      });
    },
    //修改
    update() {
      // this.checkEmpty().then(() => {
      // });
      reqseckedit({
        ...this.form,
      }).then((res) => {
        if (res.data.code == 200) {
          // （1）弹成功
          successAlert(res.data.msg);
          // (2)弹框消失
          this.cancel();
          // (3)清空数据
          this.empty();
          // (4)刷新列表
          this.reqList();
        }
      });
    },
  },
  // mixins:[toggle]
};
</script>
<style lang="less" scoped>
@import "../../../less/index.less";
.my-upload {
  width: 100px;
  height: 100px;
  border: 1px dashed @header-color;
  position: relative;
}
.my-plus {
  width: 100%;
  height: 100%;
  text-align: center;
  line-height: 100px;
  color: @primary;
}
.my-img {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
}
.my-upload input {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
}

// /deep/.avatar-uploader .el-upload {
//     border: 1px dashed #d9d9d9;
//     border-radius: 6px;
//     cursor: pointer;
//     position: relative;
//     overflow: hidden;
//   }
//   .avatar-uploader .el-upload:hover {
//     border-color: #dddddd;
//   }
//   .avatar-uploader-icon {
//     font-size: 28px;
//     color: #8c939d;
//     width: 178px;
//     height: 178px;
//     line-height: 178px;
//     text-align: center;
//   }
//   .avatar {
//     width: 178px;
//     height: 178px;
//     display: block;
//   }
</style>